package com.lanyou.esb.web.serviceMonitor;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.datadictionary.AlarmType;
import com.lanyou.esb.datadictionary.IfStatusType;
import com.lanyou.esb.exception.BundleActionException;
import com.lanyou.esb.service.ServiceAlarmStatisticsService;
import com.lanyou.esb.vo.IfAlarmStatisticsVo;
import com.lanyou.esb.vo.IfAlarmStatisticsVo.Entry;
import com.lanyou.esb.web.param.IfAlarmStatisticsParam;

/**
 * 服务告警统计控制器
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/serviceAlarmStatistics")
public class ServiceAlarmStatisticsController {
	@Autowired
	private ServiceAlarmStatisticsService serviceAlarmStatisticsService;
	@Autowired
	private ResourceLoader resourceLoader;
	@Autowired
	private UserSessionService userSessionService;

	/**
	 * 查询服务告警统计信息
	 * 
	 * @param param
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/findServiceAlarmStatisticsInfo")
	public Page<IfAlarmStatisticsVo.Entry> findServiceAlarmStatisticsInfo(
			IfAlarmStatisticsParam param, Pageable pageable) {
		if (StringUtils.isNotBlank(param.getIfName())) {
			param.setIfName("%" + param.getIfName() + "%");
		}
		IfAlarmStatisticsVo vo = serviceAlarmStatisticsService
				.findServiceAlarmStatisticsInfo(param, pageable);
		Page<IfAlarmStatisticsVo.Entry> page = new PageImpl<IfAlarmStatisticsVo.Entry>(
				vo.getEntrys(), pageable, vo.getTotal());
		return page;
	}

	/**
	 * 导出报表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws InvalidFormatException
	 * @throws SAXException
	 * @throws ParseException
	 * @throws BundleActionException
	 */
	@ResponseBody
	@RequestMapping(value = "/export", produces = "application/json")
	public void export(String param, HttpServletRequest request,
			HttpServletResponse response) throws IOException,
			InvalidFormatException, SAXException, ParseException,
			BundleActionException {
		IfAlarmStatisticsParam paramObj = GsonUtil.getDefaultGson().fromJson(
				param, IfAlarmStatisticsParam.class);
		// 获取数据
		IfAlarmStatisticsVo vo = serviceAlarmStatisticsService
				.findServiceAlarmStatisticsInfo(paramObj);
		if (vo == null) {
			vo = new IfAlarmStatisticsVo();
		}
		if (vo.getEntrys() != null) {
			String status = "";
			String type = "";
			Map<String, String> statusMap = new HashMap<>();
			statusMap.put(IfStatusType.DISABLE.ValueCode, "停用");
			statusMap.put(IfStatusType.ENABLE.ValueCode, "启用");
			Map<String, String> typeMap = new HashMap<>();
			typeMap.put(AlarmType.HTTP_400.ValueCode, "Http400错误");
			typeMap.put(AlarmType.REQUEST_OVER_LIMIT.ValueCode, "请求超限");
			typeMap.put(AlarmType.REQUEST_TIMEOUT.ValueCode, "请求超时");
			for (Entry entry : vo.getEntrys()) {
				status = statusMap.get(entry.getIfStatus());
				type = typeMap.get(entry.getAlarmType());
				entry.setIfStatus(status);
				entry.setAlarmType(type);
			}
		}
		// 设置文件名
		String fileName = new String(
				Constants.SERVICE_ALARM_STATISTICS.getBytes("gb2312"),
				"ISO8859-1");// 文件名
		InputStream in = resourceLoader.getResource(
				"classpath:jxls/templatestIn/serviceAlarmStatisticsReport.xls")
				.getInputStream();
		// 设置响应头
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + ".xls");
		OutputStream os = response.getOutputStream();
		Context context = new Context();
		context.putVar("entrys", vo.getEntrys());
		// 将数据写入输出流
		JxlsHelper.getInstance().processTemplate(in, os, context);
	}
}
